/**
 * Created by Administrator on 2017/7/31.
 */
define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var util = require('util');
	var view  = require('@base/view');
	var TYPE = {
		"FORBID": -2,
		"SYSTEM": -1,
		"CHANNEL": 1,
		"SPOT": 2,
		"DEAL": 3,
	};
	//渠道溢价
	var Channel = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'urlFile':'premium/remove',
				'urlRecover': 'premium/recover',
				'class': '',
				'view_model': {
				},
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var dom = $([
				'<div class="cl" pub-name="grid" pub-mod="admin/premium/grid/list.base"/>'
			].join(''));
			dom.appendTo(el);
			self.buildFromTemplate({
				'grid': {
					'url': 'premium/list',
					'param': {Type:TYPE.CHANNEL},
					'hasSearch': true,
					'hasFilter': true,
					'hasMenu': true
				},
			});
			this.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 150,
				'items': [
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('有效')},
							{id: 1, name: LANG('已归档')},
						],
						'value': 0
					}}
				]
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function(ev) {
			var status = ev.param.IsDeleted;
			var returnList = [];

			if(!status){
				if (pubjs.checkRight('')){
					returnList.push({'id': 'edit', 'name': LANG('编辑')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'remove','name': LANG('归档')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'saveas', 'name': LANG('另存为')});
				}

			}else{
				if (pubjs.checkRight('')){
					// 已归档的只有'还原'操作
					returnList.push( {'id': 'recover', 'name': LANG('还原')} );
				}
			}
			ev.returnValue = returnList;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var self = this;
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var Name =  ev.param[1]['Name'];
			switch (key){
				case 'remove':
					self.remove(id, Name);
					break;
				case 'recover':
					self.recover(id);
					break;
				case 'edit':
					var href = 'admin/premium/edit/';
					pubjs.controller.navigate(href + id);
					break;
				case 'saveas':
					var href = 'admin/premium/saveas/';
					pubjs.controller.navigate(href + id);
					break;
			}
		},
		//删除操作
		remove: function(id, name){
			if( !id ) {
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('真的要归档（%1）的客户账户吗?', name),function(isOk){
				if(isOk){
					pubjs.mc.send(self.getConfig('urlFile'),{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		onRemove: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('归档成功');
			this.reload();
		},
		onRemoveList: function (ev) {
			this.remove(ev.param._id, ev.param.Name);
		},
		onShowPopList: function (ev) {
			this.showPop(ev.param._id);
		},
		onRecoverList: function (ev) {
			this.recover(ev.param._id);
		},
		recover: function(id) {
			var self = this;
			pubjs.mc.send(self.getConfig('urlRecover'),{'_id': id},
				self.onRecover.bind(self)
			);
		},
		onRecover: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('还原成功');
			this.reload();
		},
		//响应右边侧边栏选项
		onSidebarFilter: function(ev){
			var self = this
				,param = {};
			util.each(ev.param, function(item, idx){
				var type = idx;
				var is = item.id;
				switch (type) {
					case 'status':
						param.IsDeleted = (is==1) ? true : false ;
						break;
				}
			});
			self.$.grid.reload(param);
		},
		load: function(){
			this.$.grid.reload();
		},
		reload: function(param){
			this.$.grid.reload(param);
		},
		showPop: function(id,isCopy){
			var self = this;
			//查看资质 弹窗
			var mod = self.get('pop');
			var c = self.getConfig();
			if(!mod){
				self.createAsync('pop', 'admin/premium/popwin/popwin.base', {}, function(module){
					if(module){
						module.show().load(id,isCopy);
					}
				});
			}else{
				mod.reset().show().load(id,isCopy);
			}
		},
		onPremiumListReload:function () {
			this.load();
		}
	});
	exports.channel = Channel;

	//广告位溢价
	var Spot = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'urlFile':'premium/remove',
				'urlRecover': 'premium/recover',
				'class': '',
				'view_model': {
				},
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var dom = $([
				'<div class="cl" pub-name="grid" pub-mod="admin/premium/grid/list.spot"/>'
			].join(''));
			dom.appendTo(el);
			self.buildFromTemplate({
				'grid': {
					'url': 'premium/list',
					'param': {Type:TYPE.SPOT},
					'hasSearch': true,
					'hasFilter': true,
					'hasMenu': true,
				},
			});
			this.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 150,
				'items': [
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('有效')},
							{id: 1, name: LANG('已归档')},
						],
						'value': 0
					}}
				]
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function(ev) {
			var status = ev.param.IsDeleted;
			var returnList = [];

			if(!status){
				if (pubjs.checkRight('')){
					returnList.push({'id': 'edit', 'name': LANG('编辑')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'remove','name': LANG('归档')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'saveas', 'name': LANG('另存为')});
				}

			}else{
				if (pubjs.checkRight('')){
					// 已归档的只有'还原'操作
					returnList.push( {'id': 'recover', 'name': LANG('还原')} );
				}
			}
			ev.returnValue = returnList;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var self = this;
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var Name =  ev.param[1]['Name'];
			switch (key){
				case 'remove':
					self.remove(id, Name);
					break;
				case 'recover':
					self.recover(id);
					break;
				case 'edit':
					var href = 'admin/premium/edit/';
					pubjs.controller.navigate(href + id);
					break;
				case 'saveas':
					var href = 'admin/premium/saveas/';
					pubjs.controller.navigate(href + id);
					break;
			}
		},
		//删除操作
		remove: function(id, name){
			if( !id ) {
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('真的要归档（%1）的客户账户吗?', name),function(isOk){
				if(isOk){
					pubjs.mc.send(self.getConfig('urlFile'),{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		onRemove: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('归档成功');
			this.reload();
		},
		onRemoveList: function (ev) {
			this.remove(ev.param._id, ev.param.Name);
		},
		onShowPopList: function (ev) {
			this.showPop(ev.param._id);
		},
		onRecoverList: function (ev) {
			this.recover(ev.param._id);
		},
		recover: function(id) {
			var self = this;
			pubjs.mc.send(self.getConfig('urlRecover'),{'_id': id},
				self.onRecover.bind(self)
			);
		},
		onRecover: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('还原成功');
			this.reload();
		},
		//响应右边侧边栏选项
		onSidebarFilter: function(ev){
			var self = this
				,param = {};
			util.each(ev.param, function(item, idx){
				var type = idx;
				var is = item.id;
				switch (type) {
					case 'status':
						param.IsDeleted = (is==1) ? true : false;
						break;
				}
			});
			self.$.grid.reload(param);
		},
		load: function(){
			this.$.grid.reload();
		},
		reload: function(param){
			this.$.grid.reload(param);
		},
		showPop: function(id,isCopy){
			var self = this;
			//查看资质 弹窗
			var mod = self.get('pop');
			var c = self.getConfig();
			if(!mod){
				self.createAsync('pop', 'admin/premium/popwin/popwin.base', {}, function(module){
					if(module){
						module.load(id,isCopy).show();
					}
				});
			}else{
				mod.reset().load(id,isCopy).show();
			}
		},
		onPremiumListReload:function () {
			this.load();
		}
	});
	exports.spot = Spot;

	//订单溢价
	var Order = view.container.extend({
		init:function(config){
			config = pubjs.conf(config, {
				'urlFile':'premium/remove',
				'urlRecover': 'premium/recover',
				'class': '',
				'view_model': {
				},
			});
			this.Super('init', arguments);
		},
		afterBuild:function(){
			var self = this;
			var c = self.getConfig();
			var el = self.getDOM();
			var dom = $([
				'<div class="cl" pub-name="grid" pub-mod="admin/premium/grid/list.deal"/>'
			].join(''));
			dom.appendTo(el);
			self.buildFromTemplate({
			});
			this.createAsync('slotSidebar', '@layout/v1/sidebar.base', {
				'target': c.targetSidebar,
				'height': 150,
				'items': [
					{'name': 'status', 'uri':'modules/sideBarBase.radio','config':{
						'title': LANG('状态：'),
						'data': [
							{id: 0, name: LANG('有效')},
							{id: 1, name: LANG('已归档')},
						],
						'value': 0
					}}
				]
			});
		},
		// 操作列点击事件
		onOperateMenuShow: function(ev) {
			var status = ev.param.IsDeleted;
			var returnList = [];

			if(!status){
				if (pubjs.checkRight('')){
					returnList.push({'id': 'edit', 'name': LANG('编辑')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'remove','name': LANG('归档')});
				}

				if (pubjs.checkRight('')){
					returnList.push({'id': 'saveas', 'name': LANG('另存为')});
				}

			}else{
				if (pubjs.checkRight('')){
					// 已归档的只有'还原'操作
					returnList.push( {'id': 'recover', 'name': LANG('还原')} );
				}
			}

			ev.returnValue = returnList;
			return false;
		},
		// 操作菜单-响应菜单操作
		onOperateMenuSelect: function(ev){
			var self = this;
			var key = ev.param[0]['key'];
			var id = ev.param[1]['_id'];
			var Name =  ev.param[1]['Name'];
			switch (key){
				case 'remove':
					self.remove(id, Name);
					break;
				case 'recover':
					self.recover(id);
					break;
				case 'edit':
					var href = 'admin/premium/edit/';
					pubjs.controller.navigate(href + id);
					break;
				case 'saveas':
					var href = 'admin/premium/saveas/';
					pubjs.controller.navigate(href + id);
					break;
			}
		},
		//删除操作
		remove: function(id, name){
			if( !id ) {
				return false;
			}
			var self = this;
			pubjs.confirm(LANG('真的要归档（%1）的客户账户吗?', name),function(isOk){
				if(isOk){
					pubjs.mc.send(self.getConfig('urlFile'),{'_id': id},
						self.onRemove.bind(self)
					);
				}
			});
		},
		onRemove: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('归档成功');
			this.reload();
		},
		onRemoveList: function (ev) {
			this.remove(ev.param._id, ev.param.Name);
		},
		onShowPopList: function (ev) {
			this.showPop(ev.param._id);
		},
		onRecoverList: function (ev) {
			this.recover(ev.param._id);
		},
		recover: function(id) {
			var self = this;
			pubjs.mc.send(self.getConfig('urlRecover'),{'_id': id},
				self.onRecover.bind(self)
			);
		},
		onRecover: function( err, data ) {
			if(err) {
				pubjs.alert(err.message);
				return false;
			}
			pubjs.alert('还原成功');
			this.reload();
		},
		//响应右边侧边栏选项
		onSidebarFilter: function(ev){
			var self = this
				,param = {};
			util.each(ev.param, function(item, idx){
				var type = idx;
				var is = item.id;
				switch (type) {
					case 'status':
						param.IsDeleted = (is==1) ? true : false;
						break;
				}
			});
			self.$.grid.reload(param);
		},
		load: function(){
			this.$.grid.reload();
		},
		reload: function(param){
			this.$.grid.reload(param);
		},
		showPop: function(id,isCopy){
			var self = this;
			//查看资质 弹窗
			var mod = self.get('pop');
			var c = self.getConfig();
			if(!mod){
				self.createAsync('pop', 'admin/premium/popwin/popwin.base', {}, function(module){
					if(module){
						module.load(id,isCopy).show();
					}
				});
			}else{
				mod.reset().load(id,isCopy).show();
			}
		},
		onPremiumListReload:function () {
			this.load();
		}
	});
	exports.order = Order;
});